package com.example.mam.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Tree implements Serializable{

    private String label;
    private Boolean isMam;
    private Long groupId;
    private List<Tree> children = new ArrayList<>();

    public Tree(String label, Boolean isMam, Long groupId, List<Tree> children) {
        this.label = label;
        this.isMam = isMam;
        this.groupId = groupId;
        this.children = children;
    }

    public Tree() {
    }

    public Tree(String label, Boolean isMam, Long groupId) {
        this.label = label;
        this.isMam = isMam;
        this.groupId = groupId;
    }

    public String getLabel() {
        return label;
    }

    public Long getGroupId() {
        return groupId;
    }

    public void setGroupId(Long groupId) {
        this.groupId = groupId;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public Boolean getMam() {
        return isMam;
    }

    public void setMam(Boolean mam) {
        isMam = mam;
    }

    public List<Tree> getChildren() {
        return children;
    }

    public void setChildren(List<Tree> children) {
        this.children = children;
    }

    @Override
    public String toString() {
        return "Tree{" +
                "label='" + label + '\'' +
                ", isMam=" + isMam +
                ", groupId=" + groupId +
                ", children=" + children +
                '}';
    }


    public static Tree getTree(String label, Boolean isMam) {
        return getTree(label, isMam, null);
    }

    public static Tree getTree(String label, Boolean isMam, Long groupId) {
        return new Tree(label, isMam, groupId);
    }

    public Tree addTree(Tree tree) {
        this.getChildren().add(tree);
        return this;
    }

    public static boolean traversalTree(Tree tree, Tree tree2) {
        System.out.println(tree);
        if (tree.getLabel().equals(tree2.getLabel())) {
            return true;
        }
        for (Tree tmp : tree.getChildren()) {
            traversalTree(tmp, tree2);

        }
        return false;
    }


    public static void main(String[] args) {
        Tree tree = Tree.getTree("root", false);
        Tree tree1 = Tree.getTree("root1", true);
        Tree tree2 = Tree.getTree("root2", false);
        Tree tree3 = Tree.getTree("root3", true);
        Tree tree4 = Tree.getTree("root4", true);
        Tree tree5 = Tree.getTree("root5", true);
        Tree tree6 = Tree.getTree("roo6", true);
        Tree tree7 = Tree.getTree("root7", true);
        Tree tree8 = Tree.getTree("root8", true);

        tree.addTree(tree1).addTree(tree2);
        tree1.addTree(tree3).addTree(tree4);
        tree2.addTree(tree5).addTree(tree6);
        tree3.addTree(tree7).addTree(tree8);


     //   System.out.println(net.sf.json.JSONObject.fromObject(tree));

        System.out.println(Tree.traversalTree(tree,Tree.getTree("root7",true)));

    }


}
